package cn.stu.cms.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.stu.cms.dto.CustomersDto;
import cn.stu.cms.dto.PageDto;
import cn.stu.cms.dto.SetTeamDto;
import cn.stu.cms.mapper.CustomersMapper;
import cn.stu.cms.pojo.Customers;
import cn.stu.cms.pojo.Teams;
import cn.stu.cms.service.CustomersService;
import cn.stu.cms.service.EmployeesService;
import cn.stu.cms.service.TeamsService;
import cn.stu.cms.service.UsersService;
import cn.stu.cms.util.BeanUtils;
import cn.stu.cms.util.R;
import cn.stu.cms.vo.EmployeeData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

import static cn.stu.cms.common.CommonConstants.*;

/**
 * 客户服务
 * (Customers)表服务实现类
 *
 * @author gh
 * @date 2023/09/19
 */
@Service
@AllArgsConstructor
public class CustomersServiceImpl extends ServiceImpl<CustomersMapper, Customers> implements CustomersService {

    private final EmployeesService employeesService;

    private final UsersService usersService;

    private final TeamsService teamsService;

    /**
     * 客户按 ID 选择
     *
     * @param customersId 客户编号
     * @return {@link R}
     */
    @Override
    public R customersSelectById(Integer customersId) {
        return R.ok(GET_INFO_OK);
    }

    /**
     * 获取页面
     *
     * @param page     页数到页
     * @param customer 客户
     * @return {@link R}<{@link Page}<{@link Customers}>>
     */
    @Override
    public R<Page<Customers>> getPage(PageDto<Customers> page, Customers customer) {
        EmployeeData employeeData = usersService.getEmployeeData();
        lambdaQuery().like(StrUtil.isNotBlank(customer.getName()), Customers::getName, customer.getName())
                .eq(StrUtil.isNotBlank(customer.getLevel()), Customers::getLevel, customer.getLevel())
                .eq(!employeeData.getIsAll(), Customers::getTeamId, employeeData.getTeamId())
                .eq(customer.getTeamId() != null, Customers::getTeamId, customer.getTeamId())
                .page(page);
        return R.ok(page, GET_INFO_OK);
    }

    @Override
    public R<Boolean> add(CustomersDto dto) {
        Teams one = teamsService.lambdaQuery()
                .eq(Teams::getTeamId, dto.getTeamId())
                .one();
        if (one == null) {
            return R.failed(ID_ERROR);
        }
        Customers customer = BeanUtils.copier(dto, Customers.class);
        save(customer);
        return R.ok(SAVE_OK_MSG);
    }

    /**
     * 编辑
     *
     * @param dto 客户
     * @return {@link R}<{@link Boolean}>
     */
    @Override
    public R<Boolean> edit(CustomersDto dto) {
        boolean exists = lambdaQuery().eq(Customers::getCustomerId, dto.getCustomerId()).exists();
        if (!exists) {
            return R.failed(ID_ERROR);
        }
        Teams one = teamsService.lambdaQuery()
                .eq(Teams::getTeamId, dto.getTeamId())
                .one();
        if (one == null) {
            return R.failed(ID_ERROR);
        }
        Customers customer = BeanUtils.copier(dto, Customers.class);
        updateById(customer);
        return R.ok(EDIT_OK_MSG);
    }

    /**
     * 删除
     *
     * @param customerId 客户ID
     * @return {@link R}<{@link Boolean}>
     */
    @Override
    public R<Boolean> delete(Long customerId) {
        boolean exists = lambdaQuery().eq(Customers::getCustomerId, customerId).exists();
        if (!exists) {
            return R.failed(ID_ERROR);
        }
        removeById(customerId);
        return R.ok(DEL_OK_MSG);
    }

    /**
     * 获取列表
     *
     * @return {@link R}<{@link List}<{@link Customers}>>
     */
    @Override
    public R<List<Customers>> getList() {
        EmployeeData employeeData = usersService.getEmployeeData();
        List<Customers> list = lambdaQuery().eq(!employeeData.getIsAll(), Customers::getTeamId, employeeData.getTeamId()).list();
        return R.ok(list, GET_INFO_OK);

    }

    /**
     * 设置团队
     *
     * @param setTeamDto 将团队数据设置为
     * @return {@link R}<{@link Boolean}>
     */
    @Override
    public R<Boolean> setTeam(SetTeamDto setTeamDto) {
        boolean teamExists = teamsService.lambdaQuery().eq(Teams::getTeamId, setTeamDto.getTeamId()).exists();
        if (!teamExists) {
            return R.failed("团队" + NO_EXIST_MSG);
        }
        boolean customerExists = lambdaQuery().eq(Customers::getCustomerId, setTeamDto.getCustomerId()).exists();
        if (!customerExists) {
            return R.failed("客户" + NO_EXIST_MSG);
        }
        lambdaUpdate().eq(Customers::getCustomerId, setTeamDto.getCustomerId()).set(Customers::getTeamId, setTeamDto.getTeamId()).update();
        return R.ok(EDIT_OK_MSG);
    }


}

